package com.swan.controllers;

import java.util.List;
import java.util.Optional;

import com.swan.beans.UserBean;
import com.swan.models.UserDao;
import com.swan.utils.CellFactory;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

/**
 * UserMgtController
 * 
 * @author xyh
 * @param <T>
 * @date 20230426
 *
 */

public class UserMgtController {
	@FXML
	private Button DelBtn;

	@FXML
	private TableView<UserBean> UserTableView;

	@FXML
	private TextField NameTextField;

	@FXML
	private TextField PhoneTextField;

	@FXML
	private ChoiceBox<String> RoleChoiceBox;

	private String[] roles = { "TEACHER", "STUDENT" };

	@FXML
	private TableColumn<UserBean, Boolean> SelectTableColumn;

	@FXML
	private TableColumn<UserBean, Integer> IdTableColumn;

	@FXML
	private TableColumn<UserBean, String> NameTableColumn;

	@FXML
	private TableColumn<UserBean, String> PhoneTableColumn;

	@FXML
	private TableColumn<UserBean, String> RoleTableColumn;

	@FXML
	private TableColumn<UserBean, String> StatusTableColumn;

	private final ObservableList<UserBean> data = FXCollections.observableArrayList();

	private UserDao dao = new UserDao();

	@FXML
	public void onDelete(ActionEvent event) {
		delete();
	}

	@FXML
	public void onAdd(ActionEvent event) {
		if (NameTextField.getText() != null && PhoneTextField.getText() != null) {
			System.out.println(NameTextField.getText());

			String name = NameTextField.getText();
			String phone = PhoneTextField.getText();
			String role = RoleChoiceBox.getValue();
			String status = "ENABLE";

			UserBean row = new UserBean(name, phone, role, status);
			dao.addUser(row);
			if (row.getId() > 0) {
				System.out.println("add user success!");
				data.add(row);
			}

			NameTextField.clear();
			PhoneTextField.clear();
		}
	}

	private boolean delete() {
		int size = data.size();
		if (size <= 0) {
			return false;
		}
		for (int i = size - 1; i >= 0; i--) {
			UserBean row = data.get(i);
			if (row.getSelected()) {
				// cannot delete admin
				if (row.getName().equals("admin")) {
					Alert alert = new Alert(Alert.AlertType.WARNING);
					alert.setContentText("admin can not be delete!");
					alert.show();
				} else {
					// confirm delete
					Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
					alert.setContentText("confirm delete user ?");
					Optional<ButtonType> showAndWait = alert.showAndWait();
					if (showAndWait.get() == ButtonType.OK) {
						data.remove(row);
						if (row.getId() > 0) {
							dao.removeUser(row.getId());
							System.out.println("delete user success!");
						}
					}
				}
			}
		}
		return true;
	}

	@FXML
	private void initialize() {
		// table view
		// set select column ui
		SelectTableColumn
				.setCellFactory(CellFactory.tableCheckBoxColumn(new Callback<Integer, ObservableValue<Boolean>>() {
					@Override
					public ObservableValue<Boolean> call(Integer index) {
						final UserBean row = UserTableView.getItems().get(index);
						ObservableValue<Boolean> ret = new SimpleBooleanProperty(row, "selected", row.getSelected());
						ret.addListener(new ChangeListener<Boolean>() {
							@Override
							public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
									Boolean newValue) {
								row.setSelected(newValue);
							}
						});
						return ret;
					}
				}));

		// set column value
		SelectTableColumn.setCellValueFactory(new PropertyValueFactory<UserBean, Boolean>("selected"));
		IdTableColumn.setCellValueFactory(new PropertyValueFactory<UserBean, Integer>("id"));
		NameTableColumn.setCellValueFactory(new PropertyValueFactory<UserBean, String>("name"));
		PhoneTableColumn.setCellValueFactory(new PropertyValueFactory<UserBean, String>("phone"));
		RoleTableColumn.setCellValueFactory(new PropertyValueFactory<UserBean, String>("role"));

		// set table view
		List<UserBean> list = dao.list();
		System.out.println("list user success!");
		data.addAll(list);

		UserTableView.setItems(data);
		UserTableView.setEditable(true);

		// new row form
		RoleChoiceBox.getItems().addAll(roles);
		RoleChoiceBox.getSelectionModel().select(0);
		RoleChoiceBox.setOnAction(this::getRole);

	}

	public String getRole(ActionEvent event) {
		String role = RoleChoiceBox.getValue();
		return role;
	}

}
